﻿using System;
using System.Web.Security;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmChangePassword : Form
    {
        UserClient _wcfUser;
        private bool init = false;

        public frmChangePassword(bool init = false)
        {
            InitializeComponent();
            LoadLanguge();
            this.KeyPreview = true;
            _wcfUser = Service.User();
            //
            this.init = init;
            lblThongBao.Visible = init;
        }

        private void LoadLanguge()
        {
            if (this.Tag != null)
            this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
        }

        private bool CheckValid()
        {
            bool returnValue = true;

            if (!txtPassword.Text.Equals(txtConfirmPassword.Text))
            {
                MessageBox.Show(Message.NewPwdOtherOldPwd, UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtConfirmPassword.Focus();
                returnValue = false;
            }
            if (txtConfirmPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show(Message.Confirm_Pwd_IsEmpty, UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtConfirmPassword.Focus();
                returnValue = false;
            }
            if (txtPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show(Message.New_Pwd_IsEmpty, UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtConfirmPassword.Focus();
                returnValue = false;
            }
            if (txtOldPass.Text.Trim() == txtPassword.Text.Trim())
            {
                MessageBox.Show("Mật khẩu mới không được trùng với mật khẩu cũ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtConfirmPassword.Focus();
                returnValue = false;
            }
            try
            {
                if (!_wcfUser.User_GetInfo(Program.strUserName).Tables[0].Rows[0]["Password"].ToString().Equals(txtOldPass.Text))
                {
                    MessageBox.Show(Message.Old_Pwd_Invalid, UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtOldPass.Focus();
                    returnValue = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show( UpdateUI.GetString("loi") + ":\n"+ex.Message+"\n" +  UpdateUI.GetString("ghiloi"));
            }
            return returnValue;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckValid())
            {
                if (_wcfUser.User_ChangePassword(Program.strUserName, txtOldPass.Text, txtPassword.Text))
                {
                    Program.strPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "SHA1");
                    Program.taskbarNotifier.Show( UIParamater.TitleProject, Message.Change_Password_Success, UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    this.Close();
                }
                else
                {
                    MessageBox.Show(Message.No_Change_Password,  UpdateUI.GetString("loi"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.init)
            {
                if (MessageBox.Show("Nếu không đổi mật khẩu mặc định, hệ thống sẽ thoát","Thông báo",MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    this.Close();
                    Application.Exit();
                }
            }
            else
            {
                this.Close();
                this.Dispose();
            }
        }

        private void frmChangePassword_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
            }
        }

        private void frmChangePassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfUser.Close();
        }
    }
}